package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.Dept;

/**
 * 部门管理 数据层
 * 
 * @author ruoyi
 */
@Mapper
public interface DeptMapper {

	/**
	 * 查询下级部门数量
	 * 
	 * @param dept 部门信息
	 * @return 结果
	 */
	@Select("<script>select count(1) from t_dept " + "where del_flag = '0' "
			+ "<if test=\"deptId != null and deptId != 0\"> and dept_id = #{deptId} </if> "
			+ "<if test=\"parentId != null and parentId != 0\"> and parent_id = #{parentId} </if></script>")
	public int selectDeptCount(Dept dept);

	/**
	 * 查询部门是否存在用户
	 * 
	 * @param deptId 部门ID
	 * @return 结果
	 */
	@Select("select count(1) from t_user where dept_id = #{deptId} and del_flag = '0'")
	public int checkDeptExistUser(Long deptId);

	/**
	 * 查询部门管理数据
	 * 
	 * @param dept 部门信息
	 * @return 部门信息集合
	 */
	@Select("<script>select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time "
			+ "from t_dept d " + "where d.del_flag = '0' "
			+ "<if test=\"deptId != null and deptId != 0\">AND dept_id = #{deptId}</if> "
			+ "<if test=\"parentId != null and parentId != 0\">AND parent_id = #{parentId}</if> "
			+ "<if test=\"deptName != null and deptName != ''\">AND dept_name like concat('%', #{deptName}, '%')</if> "
			+ "<if test=\"status != null and status != ''\">AND status = #{status}</if> " + "${params.dataScope} "
			+ "order by d.parent_id, d.order_num</script>")
	public List<Dept> selectDeptList(Dept dept);

	/**
	 * 删除部门管理信息
	 * 
	 * @param deptId 部门ID
	 * @return 结果
	 */
	@Update("update t_dept set del_flag = '2' where dept_id = #{deptId}")
	public int deleteDeptById(Long deptId);

	/**
	 * 新增部门信息
	 * 
	 * @param dept 部门信息
	 * @return 结果
	 */
	@Insert("<script>insert into t_dept(" + "<if test=\"deptId != null and deptId != 0\">dept_id,</if>"
			+ "<if test=\"parentId != null and parentId != 0\">parent_id,</if>"
			+ "<if test=\"deptName != null and deptName != ''\">dept_name,</if>"
			+ "<if test=\"ancestors != null and ancestors != ''\">ancestors,</if>"
			+ "<if test=\"orderNum != null\">order_num,</if>"
			+ "<if test=\"leader != null and leader != ''\">leader,</if>"
			+ "<if test=\"phone != null and phone != ''\">phone,</if>"
			+ "<if test=\"email != null and email != ''\">email,</if>" + "<if test=\"status != null\">status,</if>"
			+ "<if test=\"createBy != null and createBy != ''\">create_by,</if>" + "create_time" + ")values("
			+ "<if test=\"deptId != null and deptId != 0\">#{deptId},</if>"
			+ "<if test=\"parentId != null and parentId != 0\">#{parentId},</if>"
			+ "<if test=\"deptName != null and deptName != ''\">#{deptName},</if>"
			+ "<if test=\"ancestors != null and ancestors != ''\">#{ancestors},</if>"
			+ "<if test=\"orderNum != null\">#{orderNum},</if>"
			+ "<if test=\"leader != null and leader != ''\">#{leader},</if>"
			+ "<if test=\"phone != null and phone != ''\">#{phone},</if>"
			+ "<if test=\"email != null and email != ''\">#{email},</if>"
			+ "<if test=\"status != null\">#{status},</if>"
			+ "<if test=\"createBy != null and createBy != ''\">#{createBy},</if>" + "sysdate()" + ")</script>")
	public int insertDept(Dept dept);

	/**
	 * 修改部门信息
	 * 
	 * @param dept 部门信息
	 * @return 结果
	 */
	@Update("<script>update t_dept" + "<set>"
			+ "<if test=\"parentId != null and parentId != 0\">parent_id = #{parentId},</if>"
			+ "<if test=\"deptName != null and deptName != ''\">dept_name = #{deptName},</if>"
			+ "<if test=\"ancestors != null and ancestors != ''\">ancestors = #{ancestors},</if>"
			+ "<if test=\"orderNum != null\">order_num = #{orderNum},</if>"
			+ "<if test=\"leader != null\">leader = #{leader},</if>"
			+ "<if test=\"phone != null\">phone = #{phone},</if>" + "<if test=\"email != null\">email = #{email},</if>"
			+ "<if test=\"status != null and status != ''\">status = #{status},</if>"
			+ "<if test=\"updateBy != null and updateBy != ''\">update_by = #{updateBy},</if>"
			+ "update_time = sysdate()" + "</set>" + "where dept_id = #{deptId}</script>")
	public int updateDept(Dept dept);

	/**
	 * 修改子元素关系
	 * 
	 * @param depts 子元素
	 * @return 结果
	 */
	@Update("<script>update t_dept set ancestors ="
			+ "<foreach collection=\"depts\" item=\"item\" index=\"index\" separator=\" \" open=\"case dept_id\" close=\"end\">"
			+ "when #{item.deptId} then #{item.ancestors}" + "</foreach>" + "where dept_id in"
			+ "<foreach collection=\"depts\" item=\"item\" index=\"index\" separator=\",\" open=\"(\" close=\")\">"
			+ "#{item.deptId}" + "</foreach></script>")
	public int updateDeptChildren(@Param("depts") List<Dept> depts);

	/**
	 * 根据部门ID查询信息
	 * 
	 * @param deptId 部门ID
	 * @return 部门信息
	 */
	@Select("select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, "
			+ "(select dept_name from t_dept where dept_id = d.parent_id) parent_name " + "from t_dept d "
			+ "where d.dept_id = #{deptId}")
	public Dept selectDeptById(Long deptId);

	/**
	 * 校验部门名称是否唯一
	 * 
	 * @param deptName 部门名称
	 * @param parentId 父部门ID
	 * @return 结果
	 */
	@Select("select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag, d.create_by, d.create_time "
			+ "from t_dept d " + "where dept_name=#{deptName} and parent_id = #{parentId} and del_flag = '0' limit 1")
	public Dept checkDeptNameUnique(@Param("deptName") String deptName, @Param("parentId") Long parentId);

	/**
	 * 根据角色ID查询部门
	 *
	 * @param roleId 角色ID
	 * @return 部门列表
	 */
	@Select("select concat(d.dept_id, d.dept_name) as dept_name " + "from t_dept d "
			+ "left join t_role_dept rd on d.dept_id = rd.dept_id "
			+ "where d.del_flag = '0' and rd.role_id = #{roleId} " + "order by d.parent_id, d.order_num")
	public List<String> selectRoleDeptTree(Long roleId);

	/**
	 * 修改所在部门正常状态
	 * 
	 * @param deptIds 部门ID组
	 */
	@Update("<script>update t_dept set status = '0' where dept_id in "
			+ "<foreach collection=\"array\" item=\"deptId\" open=\"(\" separator=\",\" close=\")\">" + "#{deptId}"
			+ "</foreach></script>")
	public void updateDeptStatusNormal(Long[] deptIds);

	/**
	 * 根据ID查询所有子部门
	 * 
	 * @param deptId 部门ID
	 * @return 部门列表
	 */
	@Select("select * from t_dept where find_in_set(#{deptId}, ancestors)")
	public List<Dept> selectChildrenDeptById(Long deptId);

	/**
	 * 根据ID查询所有子部门（正常状态）
	 * 
	 * @param deptId 部门ID
	 * @return 子部门数
	 */
	@Select("select count(*) from t_dept where status = 0 and del_flag = '0' and find_in_set(#{deptId}, ancestors)")
	public int selectNormalChildrenDeptById(Long deptId);
}
